package com.tanhua.sso.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.sso.mapper.UserMapper;
import com.tanhua.sso.pojo.User;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;

/**
 * 发送验证码
 */

@Service
@Slf4j
public class SmsService {
//   拼接 获取redis的key， 自己设置一个code，存入redis
@Autowired
private RedisTemplate<String, String> redisTemplate;

    @Autowired
   private UserMapper userMapper;


    public boolean checkPhone(String phone){

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        //根据条件查询手机号是否存在
        userQueryWrapper.eq("mobile",phone);
        List<User> users = userMapper.selectList(userQueryWrapper);

        if (users==null){
            return false;
        }
        return true;
    }


    /**
     * 发送短信
     * @param phone
     * @return
     */
    public String sendMsg(String phone){

        String redisKey = "CODE_CHECK_"+phone;

        String code = "123456";

       this.redisTemplate.opsForValue().set(redisKey,code, Duration.ofMinutes(5));

       return "成功";
    }


}
